//
//  TBPasteboardSearchView.swift
//  LeGou
//
//  Created by lxm on 2022/7/28.
//

import UIKit

class TBPasteboardSearchView: XMBaseView {

    //MARK: --------------------属性--------------------
    var detailInfo = TBGoodsDetail()
    var linkInfo : TBLinkInfo?
    let disposeBag = DisposeBag()
    
    //MARK: --------------------UI控件--------------------
    @IBOutlet weak var goodsImg: UIImageView!
    @IBOutlet weak var goodsName: UILabel!
    @IBOutlet weak var goodsPrice: UILabel!
    @IBOutlet weak var refund: UILabel!
    @IBOutlet weak var shareBtn: UIButton!
    @IBOutlet weak var buyBtn: UIButton!
    
    //MARK: --------------------生命周期--------------------
    override init(frame: CGRect) {
        super.init(frame: frame)
        bindAction()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        bindAction()
        shareBtn.isHidden = true
        buyBtn.setTitle("去看看", for: .normal)
        buyBtn.titleLabel?.numberOfLines = 0
        buyBtn.titleLabel?.textAlignment = .center
        shareBtn.titleLabel?.numberOfLines = 0
        shareBtn.titleLabel?.textAlignment = .center
        self.close666TouchOutSideRemove = true
    }

    //MARK: --------------------UI--------------------
    func setUI(_ info: TBKouLingInfo) {
        if let data = info.tbGoods {
            detailInfo = data
            goodsImg.zm.setImage(urlStr: data.mainPic)
            goodsName.text = data.title
            goodsPrice.attributedText = "¥".zm.attributedStr(attrs: [.font : Style.FONT_Sans_SemiBold(14)])
                .zm.append(str: data.actualPrice.zm.toString(keep: 2), attri: ["f" : Style.FONT_Sans_SemiBold(22)])
            refund.text = "\(data.commission)元"
//            if data.commission > 0 {
//                shareBtn.setTitle("分享赚¥\(data.commission)", for: .normal)
//            } else {
//                shareBtn.setTitle("分享赚", for: .normal)
//            }
//            if data.buyProvince > 0 {
//                buyBtn.setTitle("自购省¥\(data.buyProvince)", for: .normal)
//            } else {
//                buyBtn.setTitle("自购省", for: .normal)
//            }
            linkInfo = info.tbLink
        }
    }
    
    //MARK: --------------------methods--------------------
    func bindAction() {
        shareBtn.rx.tap.subscribe(onNext:{ [unowned self] resp in
            if LoginService.shared.checkLogin() == false {return}
            if let data = self.linkInfo {
                let v = ZMUtil.loadNibViewFullScreen(nibName: TBSharePOPView.self)
                v.goodsInfo.accept(self.detailInfo)
                v.linkInfo.accept(data)
                APPWindow?.addSubview(v)
            }
        }).disposed(by: disposeBag)
        
        buyBtn.rx.tap.subscribe(onNext:{ [unowned self] resp in
            self.removeFromSuperview()
            let vc = TbDetailVC()
            vc.id = self.detailInfo.goodsId
            UIApplication.zm.visibleNavigationController().pushViewController(vc, animated: true)
//            if LoginService.shared.checkLogin() == false {return}
//            if let data = self.linkInfo {
//                if let url = URL(string: data.shortUrl) {
//                    if #available(iOS 10, *) {
//                        UIApplication.shared.open(url)
//                    } else {
//                        UIApplication.shared.openURL(url)
//                    }
//                }
//            }
        }).disposed(by: disposeBag)
    }
    
    @IBAction func onClose(_ sender: Any) {
        removeFromSuperview()
    }
}
